﻿/*
import com._public._filter.BitMapDataTest;
var txtFormat = new TextFormat();
txtFormat.size=15;
var bitText=new BitMapDataTest(txtFormat);
addChild(bitText)
bitText.play("像素字")
*/
package com._public._text{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class BitMapDataTest extends Sprite {
		public var play:Function;
		private var ball:Shape;
		private var myBitMapData:BitmapData;
		private var myBitmap:Bitmap;
		private var myText:TextField = new TextField;
		private var DotArray:Array = new Array();
		private var TxtFormat:TextFormat = new TextFormat;
		private var bnx:Number = 0;
		private var bny:Number = 0;
		public var showText:String;
		private var col:Number
		public function BitMapDataTest(_TxtFormat:TextFormat) {
			col=Number(_TxtFormat.color)
			play=play_fun;
			TxtFormat=_TxtFormat

			myText.defaultTextFormat = TxtFormat;
			myText.autoSize = TextFieldAutoSize.LEFT;
			myText.visible=false;
			myText.x = 0;
			myText.y = 0;
			this.addChild(myText);
		}
		private function play_fun(txt:String=""):void {
			if (txt=="") {
				return;
			}
			myText.text=txt;
			//将文本转成myBitMapData对象
			myBitMapData = new BitmapData(myText.width,myText.height,false,0xcc6600);
			myBitMapData.draw(myText);
			//填加到Bitmap
			myBitmap = new Bitmap(myBitMapData);
			myBitmap.x = bnx;
			myBitmap.y = bny;
			//显示在舞台
			//this.addChild(myBitmap); 
			//（各位可以加上看下转换成位图后的效果）；

			//将myBitMapData中有颜色的点坐标储存在数组中
			for (var y:uint = 0; y < myText.height; y++) {
				for (var x:uint = 0; x< myText.width; x++) {
					if (myBitMapData.getPixel(x,y) != 0xcc6600) {
						DotArray.push({x:x,y:y});
					}
				}
			}
			//根据数组复制点

			for (var i:uint = 0; i< DotArray.length; i++) {
				//绘制填充小球
				ball = new Shape();
				ball.graphics.beginFill(col,1);
				ball.graphics.drawCircle(0,0,1);
				this.addChild(ball);
				ball.name = String(i);
				//取随机坐标
				ball.x = Math.random()*this.stage.stageWidth;
				ball.y = Math.random()*this.stage.stageHeight;

				//trace("名字:",ball.name,"坐标:",DotArray[i].x);
				//var speed:Number = Math.random()*4;
				//var tx:Tween = new Tween(ball, "x", Back.easeOut, ball.x, DotArray[i].x+100, speed, true);
				//var ty:Tween = new Tween(ball, "y", Back.easeOut, ball.y, DotArray[i].y+400, speed, true);
				ball.addEventListener(Event.ENTER_FRAME,fun);
			}
		}
		//缓冲到指定的点坐标
		private function fun(e:Event):void {
			//缓动公式
			var speed:Number = Math.random()*10;
			e.currentTarget.x += (DotArray[uint(e.currentTarget.name)].x+bnx - e.currentTarget.x)/speed;
			e.currentTarget.y += (DotArray[uint(e.currentTarget.name)].y+bny - e.currentTarget.y)/speed;

			//删除侦听
			if (e.currentTarget.x == DotArray[uint(e.currentTarget.name)].x+bnx &&
			                                e.currentTarget.y == DotArray[uint(e.currentTarget.name)].y+bny) {
				e.currentTarget.removeEventListener(Event.ENTER_FRAME,fun);
			}
		}
	}
}